package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MstnProjectCondition;
import com.ruoyi.system.service.IMstnProjectConditionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目条件Controller
 * 
 * @author ruoyi
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/system/condition")
public class MstnProjectConditionController extends BaseController
{
    @Autowired
    private IMstnProjectConditionService mstnProjectConditionService;

    /**
     * 查询项目条件列表
     */
    @PreAuthorize("@ss.hasPermi('system:condition:list')")
    @GetMapping("/list")
    public TableDataInfo list(MstnProjectCondition mstnProjectCondition)
    {
        startPage();
        List<MstnProjectCondition> list = mstnProjectConditionService.selectMstnProjectConditionList(mstnProjectCondition);
        return getDataTable(list);
    }

    /**
     * 导出项目条件列表
     */
    @PreAuthorize("@ss.hasPermi('system:condition:export')")
    @Log(title = "项目条件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MstnProjectCondition mstnProjectCondition)
    {
        List<MstnProjectCondition> list = mstnProjectConditionService.selectMstnProjectConditionList(mstnProjectCondition);
        ExcelUtil<MstnProjectCondition> util = new ExcelUtil<MstnProjectCondition>(MstnProjectCondition.class);
        util.exportExcel(response, list, "项目条件数据");
    }

    /**
     * 获取项目条件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:condition:query')")
    @GetMapping(value = "/{projectConditionId}")
    public AjaxResult getInfo(@PathVariable("projectConditionId") Long projectConditionId)
    {
        return success(mstnProjectConditionService.selectMstnProjectConditionByProjectConditionId(projectConditionId));
    }

    /**
     * 新增项目条件
     */
    @PreAuthorize("@ss.hasPermi('system:condition:add')")
    @Log(title = "项目条件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MstnProjectCondition mstnProjectCondition)
    {
        if (!mstnProjectConditionService.checkProjectCondition(mstnProjectCondition.getProjectId(),mstnProjectCondition.getConditionType())){
            return error("已添加此类型的条件,不能重复添加");
        }
        return toAjax(mstnProjectConditionService.insertMstnProjectCondition(mstnProjectCondition));
    }

    /**
     * 修改项目条件
     */
    @PreAuthorize("@ss.hasPermi('system:condition:edit')")
    @Log(title = "项目条件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MstnProjectCondition mstnProjectCondition)
    {
        mstnProjectCondition.setConditionType(null);
        //重置状态
        mstnProjectCondition.setEditStatus("1");
        return toAjax(mstnProjectConditionService.updateMstnProjectCondition(mstnProjectCondition));
    }

    /**
     * 删除项目条件
     */
    @PreAuthorize("@ss.hasPermi('system:condition:remove')")
    @Log(title = "项目条件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectConditionIds}")
    public AjaxResult remove(@PathVariable Long[] projectConditionIds)
    {
        return toAjax(mstnProjectConditionService.deleteMstnProjectConditionByProjectConditionIds(projectConditionIds));
    }
}
